<?php 

namespace utils;

class Utils {
	
	/*
	* Recoge todos los parametros POST y los mete en un objeto
	*/
	public static function getPost(){
		$aux = new \stdClass();
		foreach ($_POST as $param_name => $param_val) {
			$aux->$param_name = $param_val;
			//echo "Param: $param_name; Value: $param_val<br />\n";
		}
		return $aux;
	}
	
	/*
	* Recoge todos los parametros GET y los mete en un objeto
	*/
	public static function GET(){
		$aux = new \stdClass();
		foreach ($_GET as $param_name => $param_val) {
			$aux->$param_name = $param_val;
		}
		return $aux;
	}
	
	/*
	* Retorna false si el string recibido contiene algun caracter que no sea letras y numeros
	*/
	public static function validarString($string){
		
		if (!preg_match('/[^A-Za-z0-9]/',$string)){
			return true;
		}
	}
	
	/*
	 * Retorna false si el string recibido contiene algun caracter que no sea letras y numeros
	 */
	public static function validarStringNoticias($string){
	
		if (!preg_match("/[^A-Za-z0-9]/",$string)){
			return true;
		}
	}

	/*
	 * Retorna false si el valor integer recibido contiene algun caracter que no sean numeros
	 */
	public static function validarInt($int){
	
		if (!preg_match('/[^0-9]/',$int)){
			return true;
		}
	}
	
	/*
	 * Recupera el contenido de un string  separado por dos variables
	 */	
	public static function getStringBetween($string, $start, $end){
	
		$string = " ".$string;
		$ini = strpos($string,$start);
		if ($ini == 0) return "";
		$ini += strlen($start);
		$len = strpos($string,$end,$ini) - $ini;
		return substr($string,$ini,$len);
	}
	
	
	public static function getSO($user_agent) {
	
		$os_platform    =   "Unknown OS Platform";
	
		$os_array       =   array(
				'/windows nt 6.3/i'     =>  'Windows 8.1',
				'/windows nt 6.2/i'     =>  'Windows 8',
				'/windows nt 6.1/i'     =>  'Windows 7',
				'/windows nt 6.0/i'     =>  'Windows Vista',
				'/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
				'/windows nt 5.1/i'     =>  'Windows XP',
				'/windows xp/i'         =>  'Windows XP',
				'/windows nt 5.0/i'     =>  'Windows 2000',
				'/windows me/i'         =>  'Windows ME',
				'/win98/i'              =>  'Windows 98',
				'/win95/i'              =>  'Windows 95',
				'/win16/i'              =>  'Windows 3.11',
				'/macintosh|mac os x/i' =>  'Mac OS X',
				'/mac_powerpc/i'        =>  'Mac OS 9',
				'/linux/i'              =>  'Linux',
				'/ubuntu/i'             =>  'Ubuntu',
				'/iphone/i'             =>  'iPhone',
				'/ipod/i'               =>  'iPod',
				'/ipad/i'               =>  'iPad',
				'/android/i'            =>  'Android',
				'/blackberry/i'         =>  'BlackBerry',
				'/webos/i'              =>  'Mobile'
		);
	
		foreach ($os_array as $regex => $value) {
	
			if (preg_match($regex, $user_agent)) {
				$os_platform    =   $value;
			}
	
		}
	
		return $os_platform;
	
	}
	
	public static function getNavegador($user_agent) {
	
		$browser        =   "Unknown Browser";
	
		$browser_array  =   array(
				'/msie/i'       =>  'Internet Explorer',
				'/firefox/i'    =>  'Firefox',
				'/safari/i'     =>  'Safari',
				'/chrome/i'     =>  'Chrome',
				'/opera/i'      =>  'Opera',
				'/netscape/i'   =>  'Netscape',
				'/maxthon/i'    =>  'Maxthon',
				'/konqueror/i'  =>  'Konqueror',
				'/mobile/i'     =>  'Handheld Browser'
		);
	
		foreach ($browser_array as $regex => $value) {
	
			if (preg_match($regex, $user_agent)) {
				$browser    =   $value;
			}
	
		}
	
		return $browser;
	
	}
	
	function unixToMySQL($timestamp){
		return date('Y-m-d H:i:s', $timestamp);
	}
	
	function formatMysqlDate($fecha){
		return date('d-m-Y H:i:s', strtotime($fecha));
	}
	
	/**
	 * @length 
	 * @uc - mayusculas default true
	 * @n - numeros, default true
	 * @sc - caracteres especiales , default false
	 */
	static function randomString($length=10,$uc=TRUE,$n=TRUE,$sc=FALSE){
	
		$source = 'abcdefghijklmnopqrstuvwxyz';
		if($uc==1) $source .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		if($n==1) $source .= '1234567890';
		if($sc==1) $source .= '|@#~$%()=^*+[]{}-_';
		if($length>0){
			$rstr = "";
			$source = str_split($source,1);
			for($i=1; $i<=$length; $i++){
				mt_srand((double)microtime() * 1000000);
				$num = mt_rand(1,count($source));
				$rstr .= $source[$num-1];
			}
		}
		return $rstr;
	}
	
	static function stringMin($string){
		return strtolower($string);
	}
	
	/*
	 * Encripta string a contraseña
	 */
	static function setPassword($pass){
		$options = [
				'cost' => 11,
				'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)
		];
		return password_hash($pass,PASSWORD_BCRYPT,$options);
	}
	
	/*
	 * @pass - contraseña sin encriptar 
	 * @pass2 - contraseña ya encriptada
	 * Verifica password, SI la validacion es correcta devuelve TRUE
	 */
	static function checkPassword($pass,$pass2){
		
		if (password_verify($pass,$pass2)) {
			return true;
		}
	}
	
	
}

?>
